spring security(二) |
您所在的位置:网站首页 › spring security login page › spring security(二) |
1.界面
简介 通过之前的章节实现了自定义登录认证,但是登录的界面是框架提供的,有时候更希望是通过自定义登录界面,接下来就来实现自定义登录界面 配置 复制spring-security-config-account项目,修改名字为spring-security-login-page 修改启动类内容如下 package com.briup.security; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @SpringBootApplication class SpringSecurityLoginPageApplication { public static void main(String[] args) { SpringApplication.run(SpringSecurityLoginPageApplication.class, args); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }修改pom.xml文件,修改的内容如下 image-20210120102150914测试 访问地址:http://127.0.0.1:9999/hello/test 跳转到默认提供的登录界面 image-20210120102332475自定义登录界面 要想实现自定义登录界面需要以下两步: 撰写登录界面 修改security配置类接下来就来挨个实现这两个步骤 撰写登录界面 在src/mainresources新建static目录,并且在该目录下新建login.html,内容如下: 登录界面 登录 用户名: 密码:了解(start) 表单的用户名和密码的name属性值必须为username和password,具体原因如下: UsernamePasswordAuthenticationFilter中,获取用户名和密码 该过滤器获取用户名密码则根据username和password获取,如下 image-20210120103920614同时还限制了其请求方式为post 了解(end) 修改配置类 找到配置类,进行修改,修改的内容如下: package com.briup.security.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired @Qualifier("myDetailService") private UserDetailsService userDetailsService; @Autowired private PasswordEncoder passwordEncoder; @Override protected void configure(HttpSecurity http) throws Exception { http.formLogin() // 表示使用表单进行登录 .loginPage("/login.html") // 表示登录界面地址 .loginProcessingUrl("/user/login") //表单登录地址 .and() // 拼接 条件 .authorizeRequests() // 设置需要认证的请求地址 .antMatchers("/","/login.html","/user/login").permitAll() // 设置 不需要认证的请求 .anyRequest().authenticated() // 任何请求都需要认证 .and() .csrf().disable(); // 关闭 security 的 csrf防护 } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { /** * 设置认证逻辑为用户自定义认证逻辑 * 设置密码加密处理器为 BCryptPasswordEncoder */ auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); } }启动测试 访问地址: http://127.0.0.1:9999/test/hello image-20210120142355279从上图可知,已经跳转到自定义登录界面,输入用户名和密码即可访问,如下 image-20210120144802243至此完成了自定义登录界面 2.结果 2.1 实现简介 从上述案例可知,当访问/test/hello,会跳转到登录界面,登录后在跳转到/test/hello地址上。 但是当直接访问登录界面进行登录时,登录后会直接跳转到/请求,如下: image-20210120145212543同时当登录失败时,页面还是跳转到登录界面,只不过是地址发生了一点点变换,如下 image-20210120145336138但实际开发过程中,更加希望不管是登录成功还是登录失败,都跳转到开发人员指定的地址或者处理器进行逻辑处理,接下来就来解决这个问题 准备工作 复制spring-security-login-page项目,修改名字为spring-security-login-result 修改pom.xml,修改部分如下图 image-20210120150557883删除.impl文件,让其重新生成 将项目设置为maven项目 修改启动类名为SpringSecurityLoginResultApplication 具体实现 解决上述问题有两种方式: 通过配置登录成功或者失败后的地址处理 通过配置登录成功或者失败后的处理器处理下面就对每种方案进行实现 自定义地址 在web层增加ResultController,内容如下: package com.briup.security.web; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/result") public class ResultController { /** * 登录成功跳转地址 * @return */ @GetMapping("/success") public String loginSuccess() { return "登录成功"; } /** * 登录失败跳转地址 * @return */ @GetMapping("/fail") public String loginFail() { return "登录失败"; } }修改配置,内容如下: package com.briup.security.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired @Qualifier("myDetailService") private UserDetailsService userDetailsService; @Autowired private PasswordEncoder passwordEncoder; @Override protected void configure(HttpSecurity http) throws Exception { http.formLogin() // 表示使用表单进行登录 .loginPage("/login.html") // 表示登录界面地址 .loginProcessingUrl("/user/login") //表单登录地址 .successForwardUrl("/result/success") // 登录成功跳转的地址 .failureForwardUrl("/result/fail") // 登录失败跳转的地址 .and() // 拼接 条件 .authorizeRequests() // 设置需要认证的请求地址 .antMatchers("/","/login.html","/user/login").permitAll() // 设置 不需要认证的请求 .anyRequest().authenticated() // 任何请求都需要认证 .and() .csrf().disable(); // 关闭 security 的 csrf防护 } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { /** * 设置认证逻辑为用户自定义认证逻辑 * 设置密码加密处理器为 BCryptPasswordEncoder */ auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); } } image-20210120152921249启动测试 当输入正确用户名密码时,效果如下: image-20210120153018296当输入错误的用户名密码,效果如下: image-20210120153047087自定义处理器 新建登录成功处理器,内容如下: package com.briup.security.handler; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.Collection; public class LoginSuccessHandler implements AuthenticationSuccessHandler { /** * @param request request对象 * @param response 响应对象 * @param authentication 身份认证对象,通过该对象可以获取用户名 * * */ @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { // 获取用户权限列表 Collection |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |